package com.thb.pay.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.thb.pay.api.payment.PayChannelRuleServiceApi;
import com.thb.pay.api.payment.vo.req.PayChannelRulePageReq;
import com.thb.pay.api.payment.vo.req.PayChannelRuleReq;
import com.thb.pay.api.payment.vo.resp.PayChannelRuleVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.entity.SuperEntity;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.PayChannelRule;
import com.thb.pay.payment.service.PayChannelRuleService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 支付-通道风控规则表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class PayChannelRuleController implements PayChannelRuleServiceApi {

    @Autowired
    private PayChannelRuleService payChannelRuleService;

    @Override
    public PageResult<PayChannelRuleVo> findPage(@RequestBody PayChannelRulePageReq pageReq) {
        IPage<PayChannelRule> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<PayChannelRule> pageResult = payChannelRuleService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, PayChannelRuleVo.class);
    }

    @Override
    public void add(@RequestBody PayChannelRuleReq payChannelRuleReq) {
        PayChannelRule payChannelRule = new PayChannelRule();
        BeanUtil.copyProperties(payChannelRuleReq, payChannelRule);
        payChannelRuleService.insert(payChannelRule);
    }

    @Override
    public void update(@RequestBody PayChannelRuleReq payChannelRuleReq) {
        PayChannelRule payChannelRule = payChannelRuleService.selectById(payChannelRuleReq.getId());
        if (payChannelRule == null) {
            throw RRException.build("passage.not.exist");
        }
        PayChannelRule targetPayChannelRule = new PayChannelRule();
        BeanUtil.copyProperties(payChannelRuleReq, targetPayChannelRule);
        payChannelRuleService.updateById(targetPayChannelRule);
    }

    @Override
    public PayChannelRuleVo getById(@RequestParam("id") Long id) {
        PayChannelRule payChannelRule = payChannelRuleService.selectById(id);
        if (payChannelRule != null) {
            PayChannelRuleVo resultData = new PayChannelRuleVo();
            BeanUtil.copyProperties(payChannelRule, resultData);
            return resultData;
        }
        return null;
    }

    @Override
    public PayChannelRuleVo getByChannelId(@RequestParam("channelId") Long channelId) {
        LambdaQueryWrapper<PayChannelRule> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayChannelRule::getChannelId, channelId);
        List<PayChannelRule> payChannelRules = payChannelRuleService.selectList(wrapper);
        if (CollUtil.isNotEmpty(payChannelRules)) {
            PayChannelRule payChannelRule = payChannelRules.get(0);
            PayChannelRuleVo resultData = new PayChannelRuleVo();
            BeanUtil.copyProperties(payChannelRule, resultData);
            return resultData;
        }
        return null;
    }

    @Override
    public void deleteBatchIds(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<PayChannelRule> wrapper = new LambdaQueryWrapper();
        wrapper.in(PayChannelRule::getId, ids);
        List<PayChannelRule> targetDbResultList = payChannelRuleService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            targetDbResultList.forEach(payType -> {
                payType.setDelFlag(SuperEntity.DEFAULT_DELETE_FLAG);
            });
            payChannelRuleService.updateBatchById(targetDbResultList);
        }
    }
}
